package com.itmk.course.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.course.entity.Course;
import com.itmk.course.entity.CourseParm;
import com.itmk.course.service.CourseService;
import com.itmk.course.service.impl.CourseServiceImpl;
import com.itmk.major.entity.Major;
import com.itmk.major.service.MajorService;
import com.itmk.major.service.impl.MajorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

///course.do的请求，都会被该servlet处理
@WebServlet("/course.do")
public class CourseServlet extends HttpServlet {
    private static final String PREX = "WEB-INF/jsp/admin";
    private CourseService courseService = new CourseServiceImpl();
    private MajorService majorService = new MajorServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("courseUI")) {
            courseUI(req, resp);
        } else if (method.equals("addUI")) {
            addUI(req, resp);
        } else if (method.equals("addSave")) {
            addSave(req, resp);
        } else if (method.equals("list")) {
            list(req, resp);
        } else if (method.equals("editUI")) {
            editUI(req, resp);
        } else if (method.equals("editSave")) {
            editSave(req, resp);
        }else if(method.equals("delete")){
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递来的id
        String courseId = req.getParameter("courseId");
        courseService.delete(courseId);
        //组装返回值
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "删除成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void editSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String courseId = req.getParameter("courseId");
        String majorId = req.getParameter("majorId");
        String courseName = req.getParameter("courseName");
        String courseNum = req.getParameter("courseNum");
        String courseDesc = req.getParameter("courseDesc");
        String courseType = req.getParameter("courseType");
        String courseCount = req.getParameter("courseCount");
        String coursePoint = req.getParameter("coursePoint");
        String address = req.getParameter("address");
        //封装参数
        Course course = new Course();
        course.setMajorId(majorId);
        course.setCourseName(courseName);
        course.setCourseNum(courseNum);
        course.setCourseDesc(courseDesc);
        course.setCourseType(courseType);
        course.setCourseCount(courseCount);
        course.setCoursePoint(coursePoint);
        course.setAddress(address);
        course.setCourseId(courseId);
        //调用service
        courseService.update(course);
        //组装返回值
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "编辑成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取编辑的id
        String courseId = req.getParameter("courseId");
        //查询所有的专业
        List<Major> editmajorAll = majorService.getAll();
        req.setAttribute("editmajorAll", editmajorAll);
        //要编辑的数据
        Course editCourse = courseService.getById(courseId);
        req.setAttribute("editCourse", editCourse);
        req.getRequestDispatcher(PREX + "/course_edit.jsp").forward(req, resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String courseName = req.getParameter("courseName");
        //封装参数
        CourseParm parm = new CourseParm();
        parm.setCourseName(courseName);
        parm.setLimit(limit);
        parm.setPage(page);
        //调用service
        List<Course> list = courseService.list(parm);
        //查询总条数
        int count = courseService.getCount(parm);
        //组装前端数据格式
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", list);
        object.put("count", count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    /**
     * 新增
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void addSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String majorId = req.getParameter("majorId");
        String courseName = req.getParameter("courseName");
        String courseNum = req.getParameter("courseNum");
        String courseDesc = req.getParameter("courseDesc");
        String courseType = req.getParameter("courseType");
        String courseCount = req.getParameter("courseCount");
        String coursePoint = req.getParameter("coursePoint");
        String address = req.getParameter("address");
        //封装参数
        Course course = new Course();
        course.setCourseId(UUID.randomUUID().toString().replaceAll("-",""));
        course.setMajorId(majorId);
        course.setCourseName(courseName);
        course.setCourseNum(courseNum);
        course.setCourseDesc(courseDesc);
        course.setCourseType(courseType);
        course.setCourseCount(courseCount);
        course.setCoursePoint(coursePoint);
        course.setAddress(address);
        //调用service
        courseService.add(course);
        //组装返回值
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "新增成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的专业
        List<Major> majorAll = majorService.getAll();
        req.setAttribute("majorAll", majorAll);
        req.getRequestDispatcher(PREX + "/course_add.jsp").forward(req, resp);
    }

    /**
     * 返回课程管理页面
     *
     * @param req
     * @param resp
     */
    private void courseUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/course.jsp").forward(req, resp);
    }
}
